Imports System.Windows.Forms
Imports System.IO
Imports System.Diagnostics
Public Class FrmMain
    Public Shared UserAccoutnID As Integer
    Public Shared UserFullName As String
    Public Shared OfficeVersion As String
    Private Sub SetPermission()
        Dim path As String = Application.StartupPath
        Dim filepath As String = Application.StartupPath & "\VirtualDesk.exe.config"

        FileACL.AddDirectorySecurity(path, "Users", _
                       Security.AccessControl.FileSystemRights.FullControl, _
                       Security.AccessControl.AccessControlType.Allow)

        FileACL.AddFileSecurity(filepath, "Users", _
                    Security.AccessControl.FileSystemRights.FullControl, _
                    Security.AccessControl.AccessControlType.Allow)


    End Sub
    Private Sub Checkupdate()
        Dim procinf As New ProcessStartInfo
        procinf.UseShellExecute = False
        procinf.FileName = Application.StartupPath & "\LiveUpdater.exe"
        procinf.WorkingDirectory = Application.StartupPath
        procinf.WindowStyle = ProcessWindowStyle.Normal
        Dim proc As New Process
        proc.StartInfo = procinf
        proc.Start()
    End Sub
    Private Sub ShowNewForm(ByVal sender As Object, ByVal e As EventArgs)
        ' Create a new instance of the child form.
        Dim ChildForm As New System.Windows.Forms.Form
        ' Make it a child of this MDI form before showing it.
        ChildForm.MdiParent = Me

        m_ChildFormNumber += 1
        ChildForm.Text = "Window " & m_ChildFormNumber

        ChildForm.Show()
    End Sub

    Private Sub OpenFile(ByVal sender As Object, ByVal e As EventArgs)
        Dim OpenFileDialog As New OpenFileDialog
        OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        OpenFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
            Dim FileName As String = OpenFileDialog.FileName
            ' TODO: Add code here to open the file.
        End If
    End Sub

    Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim SaveFileDialog As New SaveFileDialog
        SaveFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        SaveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"

        If (SaveFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
            Dim FileName As String = SaveFileDialog.FileName
            ' TODO: Add code here to save the current contents of the form to a file.
        End If
    End Sub


    Private Sub ExitToolsStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        Me.Close()
    End Sub

    Private Sub CutToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        ' Use My.Computer.Clipboard to insert the selected text or images into the clipboard
    End Sub

    Private Sub CopyToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        ' Use My.Computer.Clipboard to insert the selected text or images into the clipboard
    End Sub

    Private Sub PasteToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        'Use My.Computer.Clipboard.GetText() or My.Computer.Clipboard.GetData to retrieve information from the clipboard.
    End Sub

    Private Sub ToolBarToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        ''Me.ToolStrip.Visible = Me.ToolBarToolStripMenuItem.Checked
    End Sub

    Private Sub StatusBarToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        ''  Me.StatusStrip.Visible = Me.StatusBarToolStripMenuItem.Checked
    End Sub

    Private Sub CascadeToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        Me.LayoutMdi(MdiLayout.Cascade)
    End Sub

    Private Sub TileVerticleToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        Me.LayoutMdi(MdiLayout.TileVertical)
    End Sub

    Private Sub TileHorizontalToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        Me.LayoutMdi(MdiLayout.TileHorizontal)
    End Sub

    Private Sub ArrangeIconsToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        Me.LayoutMdi(MdiLayout.ArrangeIcons)
    End Sub

    Private Sub CloseAllToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        ' Close all child forms of the parent.
        For Each ChildForm As Form In Me.MdiChildren
            ChildForm.Close()
        Next
    End Sub

    Private m_ChildFormNumber As Integer

    Private Sub tsbRepositories_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub OpenRepositoryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenRepositoryToolStripMenuItem.Click
        Dim frmrep As New Documents()
        frmrep.MdiParent = Me
        frmrep.WindowState = FormWindowState.Normal
        frmrep.Show()
    End Sub

    Private Sub CreateEditRepositoriesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateEditRepositoriesToolStripMenuItem.Click
        Dim frmEditRep As New FrmRepositories
        frmEditRep.MdiParent = Me
        frmEditRep.Show()
    End Sub
    Private Sub SetConnectionString()
        Try
            Dim cdata As New Data
            cdata.GetConfigData()
            If cdata.UseTrust = True Then
                My.MySettings.Default("db_VirtualDeskConnectionString") = cdata.TrustConnStr
            Else
                My.MySettings.Default("db_VirtualDeskConnectionString") = cdata.ConnStr
            End If

            My.MySettings.Default.Save()


        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Virtual Desk", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Err.Clear()

        End Try
    End Sub

    Private Sub FrmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Try
        SetPermission()

        If Directory.Exists(Application.StartupPath & "\TempWork") = False Then
            Directory.CreateDirectory(Application.StartupPath & "\TempWork")
        End If
        If Directory.Exists(Application.StartupPath & "\TempView") = False Then
            Directory.CreateDirectory(Application.StartupPath & "\TempView")
        End If
        Dim officecheck As New clsOfficeCheck
        Select Case officecheck.GetComponentPath(clsOfficeCheck.OfficeComponent.Word)
            Case 2003
                OfficeVersion = "2003"
            Case 2007
                OfficeVersion = "2007"
            Case 0
                MessageBox.Show("This Application Need Office 2003 or later To Run", "Virtual Desk", MessageBoxButtons.OK, MessageBoxIcon.Error)
                OfficeVersion = "0"
        End Select
        'Dim DoLic As New MainLic
        'If DoLic.CheckLic() = False Then
        '    Application.Exit()
        '    Exit Sub
        'End If


        SetConnectionString()
        Dim frmrep As New frmLogin
        ''frmrep.MdiParent = Me
        Me.StatusStrip.Items(0).Text = "System is Locked." 'Corrected english. TMF
        frmrep.WindowState = FormWindowState.Normal
        frmrep.StartPosition = FormStartPosition.CenterScreen
        Me.Enabled = False
        If frmrep.ShowDialog() = Windows.Forms.DialogResult.Yes Then
            Me.Enabled = True
            Me.StatusStrip.Items(0).Text = UserFullName & " You Logged in " & Date.Now

            Me.LockApplicationToolStripMenuItem.Enabled = True
            Checkupdate()
        Else
            Me.LockApplicationToolStripMenuItem.Enabled = False
        End If
        ThemeMenuStrip1.Style = "Vista"

        'Catch ex As Exception
        'Err.Clear()
        'End Try
    End Sub

  

    Private Sub BackupDatabaseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackupDatabaseToolStripMenuItem.Click
        Dim frmbk As New frmDatabaseBackup
        frmbk.MdiParent = Me
        frmbk.StartPosition = FormStartPosition.CenterScreen
        '' frmbk.WindowState = FormWindowState.Maximized
        frmbk.Show()

    End Sub

    Private Sub ConfigureBackupUtilsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConfigureBackupUtilsToolStripMenuItem.Click
        Dim frmconfig As New FrmConfiguration
        frmconfig.MdiParent = Me
        frmconfig.StartPosition = FormStartPosition.CenterScreen
        frmconfig.Show()
    End Sub

    Private Sub RestoreDatabaseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RestoreDatabaseToolStripMenuItem.Click
        Dim frmrestore As New frmDatabaseRestore
        frmrestore.MdiParent = Me
        frmrestore.StartPosition = FormStartPosition.CenterScreen
        '' frmrestore.WindowState = FormWindowState.Maximized
        frmrestore.Show()
    End Sub

    Private Sub CloseApplicationToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseApplicationToolStripMenuItem.Click
        Application.Exit()
    End Sub

    
    Private Sub AccountInformactionToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AccountInformactionToolStripMenuItem.Click
        Dim frmacct As New FrmAccount
        frmacct.Acct_ID = UserAccoutnID
        frmacct.MdiParent = Me
        frmacct.StartPosition = FormStartPosition.CenterScreen
        frmacct.Show()
    End Sub

    Private Sub LockApplicationToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LockApplicationToolStripMenuItem.Click
        UserAccoutnID = 0
        Dim frmrep As New frmLogin
        ''frmrep.MdiParent = Me
        frmrep.WindowState = FormWindowState.Normal
        frmrep.StartPosition = FormStartPosition.CenterScreen
        Me.Enabled = False
        If frmrep.ShowDialog() = Windows.Forms.DialogResult.Yes Then
            Me.Enabled = True
            Me.LockApplicationToolStripMenuItem.Enabled = True
        Else
            Me.LockApplicationToolStripMenuItem.Enabled = False
        End If
        ThemeMenuStrip1.Style = "Vista"
        Me.StatusStrip.Items(0).Text = "System is Lock"
    End Sub

    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub VisitWebSiteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VisitWebSiteToolStripMenuItem.Click
        System.Diagnostics.Process.Start("http://www.digisystempr.com/")
    End Sub

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        Dim frm_about As New frmAbout
        frm_about.MdiParent = Me
        frmAbout.Show()
    End Sub

    Private Sub CheckForUpdateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckForUpdateToolStripMenuItem.Click
        Checkupdate()
    End Sub
End Class
